package com.wtwd.device.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.device.model.vo.PageDataVO;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.Medal;
import com.wtwd.device.model.entity.MedalType;
import com.wtwd.device.model.entity.MedalTypeVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.WebMedalService;
import com.wtwd.device.service.WebMedalTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mjy
 * @since 2022-04-11
 */
@Api(tags = "勋章类型模块")
@RestController
@RequestMapping("/medal-type")
public class MedalTypeController {

    private final static Logger logger = LoggerFactory.getLogger(MedalTypeController.class);

    @Autowired
    private WebMedalTypeService medalTypeService;

    @Autowired
    private WebMedalService medalService;



    /**
     * 分页获取勋章类型
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页获取勋章类型", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataTypeClass = Integer.class)
            , @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataTypeClass = Integer.class)
    })
    @GetMapping("/queryMedalTypePage")
    public RespEntity<PageDataVO<MedalTypeVo>> queryMedalTypePage(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage
            , @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        logger.info("queryMedalTypePage currentPage={},pageSize={}", currentPage, pageSize);
        Page<MedalType> page = new Page<>(currentPage, pageSize);
        IPage<MedalType> medalTypeIPage = medalTypeService.queryMedalTypePage(page);
        List<MedalType> records = medalTypeIPage.getRecords();
        List<MedalTypeVo> medalTypeVos = new ArrayList<>();
        for (MedalType e : records) {
            MedalTypeVo medalTypeVo = new MedalTypeVo();
            medalTypeVo.setCreateTime(e.getCreateTime());
            medalTypeVo.setId(e.getId());
            medalTypeVo.setType(e.getType());
            List<Medal> medals = medalService.getMedalByType(e.getType());
            medalTypeVo.setMedalNum(medals != null ? medals.size() : 0);
            medalTypeVos.add(medalTypeVo);
        }
        PageDataVO pageDataVO = new PageDataVO();
        pageDataVO.setRows(medalTypeVos);
        pageDataVO.setTotal(medalTypeVos != null ? medalTypeVos.size() : 0L);
        return new RespEntity<>(RespCode.SUCCESS,pageDataVO);
    }

    /**
     * 添加勋章类型
     *
     * @param medalType
     * @return
     */
    @ApiOperation("添加勋章类型")
    @PostMapping("/addMedalType")
    public RespEntity addMedalType(@RequestBody MedalType medalType) {
        logger.info("addMedalType param={}", medalType.toString());
        medalType.setCreateTime(System.currentTimeMillis());
        Integer rest = medalTypeService.addMedalType(medalType);
        return new RespEntity(rest>0? RespCode.SUCCESS: RespCode.FAILED);
    }

    /**
     * 删除勋章类型
     *
     * @param medalTypeId
     * @return
     */
    @ApiOperation(value = "删除勋章类型", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "medalTypeId", value = "徽章类类型id", required = true, dataTypeClass = Long.class)
    })
    @DeleteMapping("/deleteMedalType/{medalTypeId}")
    public RespEntity deleteMedalType(@PathVariable("medalTypeId") Long medalTypeId) {
        logger.info("deleteMedalType param={}", medalTypeId);
        Integer rest = medalTypeService.deleteMedalType(medalTypeId);
        return new RespEntity(rest>0? RespCode.SUCCESS: RespCode.FAILED);
    }



}

